package com.wh.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SecurityInterceptor extends HandlerInterceptorAdapter {
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		Object user = request.getSession().getAttribute("userInfo");
		if(user == null){
			System.out.println("--------aaa--------");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			StringBuilder builder = new StringBuilder();
			builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
			builder.append("alert(\"登录超时，请重新登录!\");");
			builder.append("window.top.location.href=\"");
			builder.append(request.getContextPath());
			builder.append("\"</script>");
			response.getWriter().print(builder.toString());
			response.getWriter().flush();
			response.getWriter().close();
			return false;
		}
		return true;
	}

}
